<template>
	<view>
		<!-- 头像和姓名 -->
		<view class="user-info">
			<image :src="logo" 
				style="width: 80px;height: 80px;border-radius: 50%;margin-top: 10px;margin-left: 10px;"></image>
			<text>{{user == null ? '请登录' : user.username}}</text>
		</view>
		<!-- 分割线 -->
		<view style="height: 1px;background-color: #eee;margin: 20rpx 0;"></view>
		<!-- 我的留言 -->
		<view class="myview" @click="goToView">
			<image src='../../static/my/view.png' style="width: 30px;height: 30px;margin-left: 10px;"></image>
			<text>留言</text>
		</view>
		<!-- 分割线 -->
		<view style="height: 1px;background-color: #eee;margin: 20rpx 0;"></view>
		<!-- 我的心情 -->
		<view class="mymood" @click="goToMood">
			<image src="../../static/my/mood.png" style="width: 30px;height: 30px;margin-left: 10px;"></image>
			<text>心情</text>
		</view>
	<!-- 	分割线
		<view style="height: 1px;background-color: #eee;margin: 20rpx 0;"></view>
		<!-- 我的密码 -->
		<!-- <view class="mypassword" @click="goToPassword">
			<image src="../../static/my/password.png" style="width: 30px;height: 30px;margin-left: 10px;"></image>
			<text>密码</text>
		</view> -->
		<!-- 分割线 -->
		<view style="height: 1px;background-color: #eee;margin: 20rpx 0;"></view>
		<!-- 退出登录 -->
		<view class="logout" @click="logout">
			<text>退出登录</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: '未登录',
				loggedIn: false,
				user: null,
				logo: '../../static/my/Profile photo.png'
			};
		},
		onShow() {
			console.log('onShow');
			const token = uni.getStorageSync('token')			// 同步
			
			if (token != null && token != '') {
				console.log(token);
				this.loggedIn = true
				uni.request({
					url:'http://localhost:9090/users/profile',
					method: 'GET',
					header:{
						'Authorization': token
					},
					success: (res) => {
						console.log(res);
						this.user = res.data.data;
						this.logo = this.user.avatar;
					}
				})
			}
		},
		methods: {
			goToView() {
				uni.navigateTo({
					url: '/pages/my/view'
				})
			},
			goToComment() {
				uni.navigateTo({
					url: '/pages/my/comment'
				})
			},
			goToMood() {
				uni.navigateTo({
					url: '/pages/my/mood'
				})
			},
			goToPhone() {
				uni.navigateTo({
					url: '/pages/my/username'
				})
			},
			goToPassword() {
				uni.navigateTo({
					url: '/pages/my/password'
				})
			},
			logout() {
				uni.removeStorageSync('token'); // 清除本地存储的 token
				this.user = null; // 重置用户信息
				this.loggedIn = false; // 设置未登录状态
				uni.showToast({
					title: '已退出登录',
					icon: 'success'
				});
				uni.reLaunch({
					url: '/pages/login/login' // 跳转到登录页面
				});
			}
		}
	};
</script>

<style>
	.user-info {
		display: flex;
		align-items: center;
	}

	.section {
		margin-top: 20rpx;
	}

	.logout {
		position: fixed;
		bottom: 60px;
		left: 0;
		width: 100%;
		background-color: #007aff; /* 蓝色背景 */
		color: white; /* 白色文字 */
		text-align: center;
		padding: 15px 0;
		font-size: 16px;
	}
</style>